/**
 * 药品库
 */
Lib = cc.Sprite.extend({
	libArr: [],
	openFlag:false,
	doing:false,
	rm : 5,
	ctor:function(p){
		this._super("#lib_back.png");
		p.addChild(this,30);
		this.setTag(TAG_LIB);
	},
	init:function(tags){
		this.libArr = [];
		this.openFlag = false;
		this.setPosition(gg.width + this.width * 0.5 + 9,550 + 30);
		var first = true;
		var firstPos = cc.p(30, this.height * 0.5);
		var prev = null;
		for(var i in tags){
			var rel = this.getLibRel(tags[i]);
			var lib = new LibButton(this, 10, rel.tag, rel.img, this.callback);
			if(first){
				first = false;
				lib.setPosition(firstPos);
			} else {
				lib.right(prev, this.rm)
			}
			prev = lib;
		}
	},
	getLibRel:function(tag){
		for(var i in libRelArr){
			if(libRelArr[i].tag == tag){
				return libRelArr[i];
			}
		}
	},
	moveLib:function(tag,width){
		width = 75;
		var begin = false;
		for(var i in this.libArr){
			var libTag = this.libArr[i];
			if(tag == libTag){
				begin = true;
			}
			if(begin){
				var lib = this.getChildByTag(libTag);
				if(lib != null){
					lib.runAction(cc.moveBy(0.5,cc.p(-width, 0)));
				}
			}
		}
	},
	callback:function(p){
		var pos = this.getPosition(); 
		var action = gg.flow.flow.action;
		switch(p.getTag()){
			case TAG_LIB_PAPER3:
				ll.run.loadPaper3(pos);
				break;
			case TAG_LIB_BOX:
				ll.run.loadBox(pos);
				break;
			case TAG_LIB_SALT:
				ll.run.loadSalt(pos);
				break;
			case TAG_LIB_CYLINDER:
				ll.run.loadCyl(pos);
				break;
			case TAG_LIB_KETTLE:
				ll.run.loadKettle(pos);
				break;
			case TAG_LIB_PAPER:
				ll.run.showPaper(pos)
				break;
			case TAG_LIB_IRON:
				ll.run.loadIron(pos);
				break;
			case TAG_LIB_BEAKER:
				ll.run.loadBeaker(pos);	
				break;
			case TAG_LIB_FUNNEL:
				ll.run.loadFunnel(pos);
				break;
			case TAG_LIB_ROD:
				ll.run.loadRod(pos);
				break;
			case TAG_LIB_LAMP:
				ll.run.loadLamp(pos);
				break;
			case TAG_LIB_MATCH:
				ll.run.loadMatch(pos);
				break;
			case TAG_LIB_SHELF:
				ll.run.loadShelf(pos);
				break;
			case TAG_LIB_NET:
				ll.run.loadNet(pos);
				break;
			case TAG_LIB_DROP:
			case TAG_LIB_BACL2:
			case TAG_LIB_NAOH:
			case TAG_LIB_NA2CO3:
			case TAG_LIB_HCl:
			case TAG_LIB_ALCOHOL:
				ll.run.loadDrop(pos);
				break;
			case TAG_LIB_POT:
				ll.run.loadPot(pos);
				break;
			case TAG_LIB_PH:
				ll.run.loadPH(pos);
				break;
			case TAG_LIB_AIR:
				ll.run.loadAir(pos);
				break;
			case TAG_LIB_SPOON:
				ll.run.loadSpoon(pos);
				break;
			case TAG_LIB_PAPER2:
				ll.run.loadPaper2(pos);
				break;
			case TAG_LIB_WIDE:
				ll.run.loadWide(pos);
			break;
			default:
				break;
		}
		if(action == ACTION_NONE){
			this.moveLib(p.getTag(), p.width * p.getScale());
			p.removeFromParent(true);
		}
	},
	isOpen:function(){
		return this.openFlag; 
	},
	open:function(){
		if(this.openFlag || this.doing){
			return;
		}
		this.doing = true;
		var move = cc.moveBy(0.4, cc.p(-this.width,0));
		var func = cc.callFunc(function(){
			this.openFlag = true;
			this.doing = false;
			var tag = gg.flow.flow.tag;
			 if(tag instanceof Array){
				 if(TAG_LIB_MIN < tag[1]){
					 // 显示箭头
					 gg.flow.location();
				 }
			 }
		}, this);
		var seq = cc.sequence(move,func);
		this.runAction(seq);
	},
	close:function(){
		if(!this.openFlag || this.doing){
			return;
		}
		this.doing = true;
		var move = cc.moveBy(0.4, cc.p(this.width,0));
		var func = cc.callFunc(function(){
			this.openFlag = false;
			this.doing = false;
			var tag = gg.flow.flow.tag;
			if(tag instanceof Array){
				if(TAG_LIB_MIN < tag[1]){
//					// 隐藏箭头
//					ll.tip.arr.out();
					//箭头指向按钮
					gg.flow.location();
				}
			}
		}, this);
		var seq = cc.sequence(move,func);
		this.runAction(seq);
	}
});
libRelArr = [
     {tag:TAG_LIB_MIN, name:""},
     {tag:TAG_LIB_PAPER3, name:"滤纸",img:"#paper.png"},
     {tag:TAG_LIB_BOX, name:"砝码",img:"#famahe.png"},
     {tag:TAG_LIB_SALT, name:"粗盐",img:"#salt4.png"},
     {tag:TAG_LIB_SPOON, name:"药匙",img:"#spoon3.png"},
     {tag:TAG_LIB_KETTLE, name:"蒸馏水瓶",img:"#kettle.png"},
     {tag:TAG_LIB_PAPER, name:"折叠滤纸",img:"#filter_paper2.png"},
     {tag:TAG_LIB_IRON, name:"铁架",img:"#ironShelf.png"},
     {tag:TAG_LIB_FUNNEL, name:"漏斗",img:"#funnel.png"},
     {tag:TAG_LIB_ROD, name:"玻璃棒",img:"#rod.png"},
     {tag:TAG_LIB_BEAKER, name:"烧杯",img:"#beaker/beaker.png"},
     {tag:TAG_LIB_LAMP, name:"酒精灯",img:"#lamp/lamp.png"},
     {tag:TAG_LIB_SHELF, name:"三脚架",img:"#shelf.png"},
     {tag:TAG_LIB_NET, name:"石棉网",img:"#asbestosNet.png"},
     {tag:TAG_LIB_BACL2, name:"氯化钡",img:"#drop_bottle0.png"},
     {tag:TAG_LIB_NAOH, name:"氢氧化钠",img:"#drop_bottle3.png"},
     {tag:TAG_LIB_NA2CO3, name:"碳酸钠",img:"#drop_bottle0.png"},
     {tag:TAG_LIB_HCl, name:"盐酸",img:"#drop_bottle0.png"},
     {tag:TAG_LIB_ALCOHOL, name:"酒精",img:"#drop_bottle0.png"},
     {tag:TAG_LIB_DROP, name:"滴瓶",img:"#drop_bottle0.png"},
     {tag:TAG_LIB_CYLINDER, name:"量筒",img:"#cylinder.png"},
     {tag:TAG_LIB_POT, name:"蒸发皿",img:"#pot.png"},
     {tag:TAG_LIB_PH, name:"pH试纸",img:"#color_card.png"},
     {tag:TAG_LIB_AIR, name:"空气压缩机",img:"#air.png"},
     {tag:TAG_LIB_PAPER2, name:"滤纸",img:"#paper.png"},
     {tag:TAG_LIB_MATCH_BOX, name:"火柴盒",img:"#lamp/match_box.png"},
     {tag:TAG_LIB_WIDE, name:"广口瓶",img:"#salt4.png"},
     {tag:TAG_LIB_MATCH, name:"火柴",img:"#lamp/match.png"}];

